import 'package:flutter_test/flutter_test.dart';
import 'package:awak/common/models/content.dart';
import 'package:awak/common/models/review.dart';
import 'package:awak/common/models/user.dart';
import 'package:awak/page/community/content_data_holder.dart';

void main() {
  group('Content Review Integration Tests', () {
    late Content testContent;
    late List<Review> testReviews;
    late List<User> testUsers;

    setUp(() {
      // 重置状态
      ContentDataHolder.clearCurrentContent();
      ContentDataHolder.allContents.clear();

      // 创建测试数据
      testContent = Content(
        id: 1,
        picture: 'content1.jpg',
        description: 'This is a philosophical content about reality and existence.',
        userId: 1,
        topicId: 1,
      );

      testUsers = [
        User(
          id: 1,
          username: 'alpha',
          password: 'password',
          nickname: 'Alpha User',
          avatar: 'avatar1.jpg',
          signature: 'Philosophy enthusiast',
        ),
        User(
          id: 2,
          username: 'bravo',
          password: 'password',
          nickname: 'Bravo User',
          avatar: 'avatar2.jpg',
          signature: 'Deep thinker',
        ),
        User(
          id: 3,
          username: 'charlie',
          password: 'password',
          nickname: 'Charlie User',
          avatar: 'avatar3.jpg',
          signature: 'Wisdom seeker',
        ),
      ];

      testReviews = [
        Review(
          id: 1,
          content: 'This is such a profound perspective on philosophy! Really makes me think about the nature of existence.',
          userId: 2,
          contentId: 1,
          createdAt: DateTime.now().subtract(Duration(hours: 1)),
        ),
        Review(
          id: 2,
          content: 'I love how you explained the complexity of philosophical thinking. Very insightful!',
          userId: 3,
          contentId: 1,
          createdAt: DateTime.now().subtract(Duration(minutes: 30)),
        ),
        Review(
          id: 3,
          content: 'Philosophy has always fascinated me. Thanks for sharing this perspective!',
          userId: 1,
          contentId: 1,
          createdAt: DateTime.now().subtract(Duration(minutes: 15)),
        ),
      ];

      ContentDataHolder.allContents = [testContent];
    });

    test('Review model should include contentId field', () {
      final review = Review(
        id: 1,
        content: 'Test review content',
        userId: 1,
        contentId: 1,
        createdAt: DateTime.now(),
      );

      expect(review.contentId, 1);
      expect(review.content, 'Test review content');
      expect(review.userId, 1);
      expect(review.createdAt, isA<DateTime>());
    });

    test('Review toMap should include contentId', () {
      final review = Review(
        id: 1,
        content: 'Test review',
        userId: 1,
        contentId: 2,
        createdAt: DateTime.now(),
      );

      final map = review.toMap();
      expect(map['contentId'], 2);
      expect(map['content'], 'Test review');
      expect(map['userId'], 1);
      expect(map['createdAt'], isA<int>());
    });

    test('Review fromMap should include contentId', () {
      final now = DateTime.now();
      final map = {
        'id': 1,
        'content': 'Test review',
        'userId': 1,
        'contentId': 3,
        'createdAt': now.millisecondsSinceEpoch,
      };

      final review = Review.fromMap(map);
      expect(review.contentId, 3);
      expect(review.content, 'Test review');
      expect(review.userId, 1);
      expect(review.createdAt?.millisecondsSinceEpoch, now.millisecondsSinceEpoch);
    });

    test('Review copyWith should handle contentId', () {
      final original = Review(
        id: 1,
        content: 'Original content',
        userId: 1,
        contentId: 1,
        createdAt: DateTime.now(),
      );

      final updated = original.copyWith(
        contentId: 2,
        content: 'Updated content',
      );

      expect(updated.contentId, 2);
      expect(updated.content, 'Updated content');
      expect(updated.userId, 1); // 未更改的字段保持原值
    });

    test('should associate reviews with correct content', () {
      // 验证评论与内容的关联
      for (final review in testReviews) {
        expect(review.contentId, testContent.id);
      }

      // 验证内容有多个评论
      final contentReviews = testReviews.where((r) => r.contentId == testContent.id).toList();
      expect(contentReviews.length, 3);
    });

    test('should associate reviews with correct users', () {
      // 验证评论与用户的关联
      for (final review in testReviews) {
        final user = testUsers.where((u) => u.id == review.userId).firstOrNull;
        expect(user, isNotNull);
        
        if (review.userId == 1) {
          expect(user?.nickname, 'Alpha User');
        } else if (review.userId == 2) {
          expect(user?.nickname, 'Bravo User');
        } else if (review.userId == 3) {
          expect(user?.nickname, 'Charlie User');
        }
      }
    });

    test('ContentDataHolder should manage current content', () {
      // 测试设置当前内容
      ContentDataHolder.setCurrentContent(testContent);
      expect(ContentDataHolder.currentContent, testContent);
      expect(ContentDataHolder.currentContent?.id, 1);

      // 测试清除当前内容
      ContentDataHolder.clearCurrentContent();
      expect(ContentDataHolder.currentContent, null);

      // 测试根据ID获取内容
      final foundContent = ContentDataHolder.getContentById(1);
      expect(foundContent, testContent);
      expect(foundContent?.description, contains('philosophical'));
    });

    test('should handle review chronological order', () {
      // 按创建时间排序评论
      testReviews.sort((a, b) => b.createdAt!.compareTo(a.createdAt!));
      
      // 验证最新的评论在前面
      expect(testReviews.first.content, contains('Philosophy has always fascinated'));
      expect(testReviews.last.content, contains('profound perspective'));
    });

    test('should support content navigation flow', () {
      // 模拟从社区页面点击内容到详情页的流程
      
      // 1. 设置当前内容
      ContentDataHolder.setCurrentContent(testContent);
      expect(ContentDataHolder.currentContent, isNotNull);
      
      // 2. 验证内容数据完整性
      final currentContent = ContentDataHolder.currentContent!;
      expect(currentContent.picture.isNotEmpty, true);
      expect(currentContent.description.isNotEmpty, true);
      expect(currentContent.userId, greaterThan(0));
      expect(currentContent.topicId, greaterThan(0));
      
      // 3. 验证可以获取相关评论
      final contentReviews = testReviews.where((r) => r.contentId == currentContent.id).toList();
      expect(contentReviews.isNotEmpty, true);
    });

    test('should handle new review creation', () {
      final newReview = Review(
        content: 'This is a new review for testing',
        userId: 2,
        contentId: testContent.id!,
        createdAt: DateTime.now(),
      );

      // 验证新评论的属性
      expect(newReview.content.isNotEmpty, true);
      expect(newReview.userId, 2);
      expect(newReview.contentId, testContent.id);
      expect(newReview.createdAt, isA<DateTime>());
    });

    test('should maintain data consistency between content and reviews', () {
      // 验证数据一致性
      final contentId = testContent.id!;
      
      // 所有测试评论都应该关联到同一个内容
      for (final review in testReviews) {
        expect(review.contentId, contentId);
      }
      
      // 内容应该有对应的评论
      final relatedReviews = testReviews.where((r) => r.contentId == contentId).toList();
      expect(relatedReviews.length, greaterThan(0));
      
      // 评论的用户ID应该在用户列表中存在
      for (final review in relatedReviews) {
        final userExists = testUsers.any((u) => u.id == review.userId);
        expect(userExists, true);
      }
    });
  });
}
